package org.neuedu.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.neuedu.JWT.JwtUtil;
import org.neuedu.api.FileUploadService;
import org.neuedu.entity.Employee;
import org.neuedu.exception.LzwException;
import org.neuedu.mapper.EmployeeMapper;
import org.neuedu.mapper.SchedulingMapper;
import org.neuedu.service.EmployeeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzw
 * @since 2024-11-07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Resource
    FileUploadService fileUploadService;
    @Resource
    SchedulingMapper schedulingMapper;
    @Resource(name="lzwredis")
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public IPage<Employee> list(Integer pageNo, Integer pageSize, String name) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("realname", name);
        }
        return this.page(new Page<>(pageNo, pageSize), wrapper);
    }

    @Override
    public Boolean check(String field, String value, Long id) {
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(field,value);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return this.count(queryWrapper)==0;
    }

    @Override
    public boolean add(Long deptmentId, Long registLevelId, Long schedulingId, String realname, String phone, String email, String password, MultipartFile file) {
        return this.save(new Employee(
                realname,
                bCryptPasswordEncoder.encode(password) ,
                deptmentId,
                registLevelId,
                schedulingId,
                email,
                phone,
                fileUploadService.upload(file)
        ));
    }



    @Override
    public Boolean del(Long id, Boolean delmark) {
        return this.updateById(new Employee(id,delmark));
    }

    @Override
    public Employee getByPhone(String phone) {
        QueryWrapper<Employee> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        return this.getOne(wrapper);
    }

    @Override
    public boolean updateEmpById(Employee data,String oldpassword,MultipartFile file) {
        if (StringUtils.isNotBlank(oldpassword)){
            Employee emp=this.getById(data.getId());
            if (bCryptPasswordEncoder.matches(oldpassword, emp.getPassword())){
                data.setPassword(bCryptPasswordEncoder.encode(data.getPassword()));
                return this.updateById(data);
            }
            return false;
        } else if(!ObjectUtils.isEmpty(file) && !(file.getSize() <= 0)) {
            data.setIcon(fileUploadService.upload(file));
            return this.updateById(data);
        }else{
            return this.updateById(data);
        }
    }

    @Override
    public List<Employee> getRegisterEmp(Integer week, String noon, Long deptId, Long registLevelId) {
        int weishu="AM".equals(noon)? week*2 +1:week*2+1;
        List<Integer> schedulings=schedulingMapper.getIdByWeek(weishu);
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("deptment_id",deptId)
                .eq("regist_level_id",registLevelId)
                .in("scheduling_id",schedulings);
        return this.list(queryWrapper);
    }

    @Override
    public String login(String phone, String password) {
        System.out.println(phone);
        System.out.println(password);
        QueryWrapper <Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Employee emp=this.getOne(queryWrapper);
        if (null==emp || !bCryptPasswordEncoder.matches(password, emp.getPassword())){
            throw new LzwException("用户名或密码错误");
        }
        if (!emp.getDelmark()){
            throw new LzwException("用户名已被禁用");
        }
        redisTemplate.opsForValue().set("loginId::" + emp.getId(), emp.getRealname(), 8, TimeUnit.HOURS);
        return JwtUtil.createToken(emp);
    }

    @Override
    public Boolean loginout(String phone) {
        QueryWrapper <Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Employee emp=this.getOne(queryWrapper);
        redisTemplate.delete("loginId::" + emp.getId());
        return true;
    }

}
